﻿using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Linq;
using System.Web;
using Librerias;

namespace Aplicacion.Modelos
{
    ///<summary>
    ///</summary>
    public class UsuarioGrid
    {
        public int ID { get; set; }
        public string GUID { get; set; }
        public string Usuario { get; set; }
        public string Nombre { get; set; }
        public string Email { get; set; }
        public string Acciones { get; set; }

        private static List<UsuarioGrid> lista = new List<UsuarioGrid>();

        public static List<UsuarioGrid> Buscar(string str, int origen, int limite)
        {
            var ad = new ObjetoAd();
            SearchResultCollection results = ad.Buscar(str);
            var i = 0;


            for (int j = origen; j < limite; j++)
            {
                if (j == results.Count)
                    break;

                SearchResult sr = results[j];
                var rad = new ObjetoAd(sr.Path);
                lista.Add(new UsuarioGrid
                {
                    ID = i,
                    GUID = new Guid((byte[])rad.AdGuid).ToString(),
                    Usuario = rad.SamAccountName,
                    Nombre = rad.DisplayName,
                    Email = rad.Mail,
                    Acciones = new Guid((byte[])rad.AdGuid).ToString()
                }
                );
            }


            /*
            foreach (SearchResult r in results)
            {
                var rad = new ObjetoAd(r.Path);
                lista.Add(new UsuarioGrid
                              {
                                  ID = i,
                                  GUID = new Guid((byte[])rad.AdGuid).ToString(),
                                  Usuario = rad.SamAccountName,
                                  Nombre = rad.DisplayName,
                                  Email = rad.Mail,
                                  Acciones = new Guid((byte[])rad.AdGuid).ToString()
                              }
                    );
                i++;
            }*/
            return lista;
        }
    }



}